package com.mtnz.njk.Service;

import com.mtnz.njk.Mapper.*;
import com.mtnz.njk.entity.*;
import com.mtnz.njk.model.*;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 用户购物车服务层
 * @Author: FBL
 * @CreateDate: 2019\7\20 0020 10:19
 * @Version: 1.0
 */
@Service
public class CartService {
    @Autowired
    CartMapper cartMapper;

    @Autowired
    CartProductMapper cartProductMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductRemarkMapper productRemarkMapper;

    @Autowired
    ProductActiveTypeMapper activeTypeMapper;

    @Autowired
    ProductActiveThresholdMapper productActiveThresholdMapper;

    @Autowired
    CouponTypeMapper couponTypeMapper;

    @Autowired
    OrderProductMapper orderProductMapper;


    /**
     * 查询用户购物车数量(有几个商品规格)
     *
     * @param userId
     * @return
     */
    public ResponseResult countCartProduct(String userId) throws Exception {
        // 查询该用户的购物车数量
        Example example = new Example(CartEntity.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("delFlag", "0");
        CartEntity cartEntity = cartMapper.selectOneByExample(example);
        int carNum = 0;
        if (cartEntity != null) {
            Example cartProductExample = new Example(CartProductEntity.class);
            cartProductExample.createCriteria().andEqualTo("cartId", cartEntity.getCartId());
            List<CartProductEntity> cartProductEntities = cartProductMapper.selectByExample(cartProductExample);
            carNum = cartProductEntities.size();
        }
        return new ResponseResult(ResponseMessage.OK, carNum);
    }

    /**
     * 用户购物车详情
     *
     * @param userId
     * @return
     */
    public ResponseResult userCartList(String userId, String orderInfoId) throws Exception {

        List<CartProductEntity> cartProductEntities = new ArrayList<>();
        CartEntity cart = new CartEntity();
        if (orderInfoId != null && !("").equals(orderInfoId)) {
            // 查询订单商品数据
            Example orderProductExample = new Example(OrderProductEntity.class);
            orderProductExample.createCriteria().andEqualTo("orderInfoId", orderInfoId);
            List<OrderProductEntity> orderProductEntities = orderProductMapper.selectByExample(orderProductExample);
            for (OrderProductEntity orderProductEntity : orderProductEntities) {
                CartProductEntity cartProductEntity = new CartProductEntity();
                cartProductEntity.setProductId(orderProductEntity.getProductId());
                cartProductEntity.setProductRemarkId(orderProductEntity.getRemarkId());
                cartProductEntity.setCartBuyNum(Integer.valueOf(orderProductEntity.getNumber()));
                cartProductEntity.setSelectFlag(1);
                cartProductEntity.setProductStatus(0);
                cartProductEntities.add(cartProductEntity);
            }
        } else {
            // 获取用户购物车
            Example carExample = new Example(CartEntity.class);
            carExample.createCriteria().andEqualTo("userId", userId).andEqualTo("delFlag", 0);
            cart = cartMapper.selectOneByExample(carExample);
            if (cart == null) {
                return new ResponseResult(ResponseMessage.FAIL, "用户购物车为空");
            }
            // 查询购物车中的商品id和商品规格id
            Example example = new Example(CartProductEntity.class);
            example.createCriteria().andEqualTo("cartId", cart.getCartId());
            cartProductEntities = cartProductMapper.selectByExample(example);
            if (cartProductEntities == null || cartProductEntities.size() == 0) {
                return new ResponseResult(ResponseMessage.FAIL, "用户购物车为空");
            }
        }

        // 购物车数据模型
        CartMessageModel cartMessageModel = new CartMessageModel();
        // 返回商品list结果集
        ArrayList<ProductEntity> productEntities = new ArrayList<>();
        // 选中购物车商品总计金额
        double countMoney = 0.0;
        // 买几送几商品集合
        ArrayList<ProductRemarkEntity> buyGives = new ArrayList<>();
        Integer shareBillRemarks = 0;
        // 送代金券集合
        ArrayList<CouponTypeEntity> couponEntities = new ArrayList<>();
        Boolean shareBillGiveBuy = false;
        Boolean shareBillCoupon = false;
        ProductRemarkEntity productRemarkEntity = new ProductRemarkEntity();

        for (CartProductEntity cartProduct : cartProductEntities) {

            // 查询该商品
            Example productExample = new Example(ProductEntity.class);
            productExample.createCriteria().andEqualTo("productId", cartProduct.getProductId());
            ProductEntity productEntity = productMapper.selectOneByExample(productExample);

            // 查询该规格
            Example productRemarkExample = new Example(ProductRemarkEntity.class);
            productRemarkExample.createCriteria().andEqualTo("remarkId", cartProduct.getProductRemarkId());
            productRemarkEntity = productRemarkMapper.selectOneByExample(productRemarkExample);
            productRemarkEntity.setCartProductId(cartProduct.getCartProductId());
            productRemarkEntity.setProductStatus(cartProduct.getProductStatus());
            // 置入该商品规格的选中状态
            productRemarkEntity.setSelectFlag(cartProduct.getSelectFlag());
            // 该商品被选中并且该商品不是换购商品
            if (cartProduct.getSelectFlag() == 1 ) {
                // 单价 x 数量
                countMoney += Double.valueOf(productRemarkEntity.getPrice()) * cartProduct.getCartBuyNum();
            }
            // 该商品规格属于换购商品
            if (cartProduct.getProductStatus() == 1) {
                // 查询该换购商品的阈值id
                String remark = cartProduct.getProductRemarkId();
                ProductRemarkEntity remarkEntity = productRemarkMapper.selectByPrimaryKey(remark);
                Integer thresholdId = remarkEntity.getThresholdId();
                ProductActiveThresholdEntity productActiveThresholdEntity = productActiveThresholdMapper.selectByPrimaryKey(thresholdId);
                // 阈值三 换购价 阈值二 换购数量   换购价*换购件数
                countMoney += productActiveThresholdEntity.getThresholdTwo();
            }

            List<ProductRemarkEntity> productRemarkEntities = new ArrayList<>();
            productRemarkEntities.add(productRemarkEntity);
            // 将该规格信息置入该商品
            productEntity.setProductRemarkEntities(productRemarkEntities);
            // 查询该商品规格的阈值
            ProductActiveThresholdEntity productActiveThresholdEntity = productActiveThresholdMapper.selectByPrimaryKey(productRemarkEntity.getThresholdId());
            // 有活动 为空没有活动
            if (productActiveThresholdEntity != null) {
                // 查询该活动类型
                Example activeTypeExample = new Example(ProductActiveThresholdEntity.class);
                activeTypeExample.createCriteria().andEqualTo("activeTypeId", productActiveThresholdEntity.getActiveTypeId());
                ProductActiveTypeEntity productActiveTypeEntity = activeTypeMapper.selectOneByExample(activeTypeExample);
                //  该活动已开始
                if (new Date().after(productActiveThresholdEntity.getBeginTime()) && new Date().before(productActiveThresholdEntity.getEndTime())) {
                    int activeTypeId = productActiveTypeEntity.getActiveTypeId();
                    // 自动触发型活动
                    switch (activeTypeId) {
                        // 买几送几
                        case 1:
                            // 重新设置活动名字
                            productActiveTypeEntity.setActiveTypeName(productActiveThresholdEntity.getActiveName());
                            // 该商品允许拼单
                            if (productRemarkEntity.getShareBill() == 1) {
                                // 原来拼单商品置入
                                buyGives.add(productRemarkEntity);
                                shareBillRemarks += cartProduct.getCartBuyNum();
                                // 查询那些商品可以与该商品拼单
                                String shareBillRemarkId = productRemarkEntity.getShareBillRemarkId();
                                for (CartProductEntity cartRemark : cartProductEntities) {
                                    // 该商品可以拼单
                                    if (shareBillRemarkId.equals(cartRemark.getProductRemarkId())) {
                                        // 可拼单商品置入
                                        buyGives.add(productRemarkMapper.selectByPrimaryKey(shareBillRemarkId));
                                        shareBillRemarks += cartRemark.getCartBuyNum();
                                    }
                                }
                            }

                            // 拼单商品选择赠送商品规格(价格最低的商品)（覆盖单体满足要求赠送的）
                            if (buyGives != null && shareBillRemarks >= productActiveThresholdEntity.getThresholdOne()) {
                                shareBillGiveBuy = true;
                                double minPrice = buyGives.stream().mapToDouble(s -> Integer.valueOf(s.getPrice())).min().getAsDouble();
                                int countBuyNum = 0;
                                for (ProductRemarkEntity buyGive : buyGives) {
                                    for (CartProductEntity cartProductEntity : cartProductEntities) {
                                        if (buyGive.getRemarkId().equals(cartProductEntity.getProductRemarkId())) {
                                            countBuyNum += cartProductEntity.getCartBuyNum();
                                        }
                                    }
                                }
                                for (ProductRemarkEntity buyGiveEntity : buyGives) {
                                    if (Integer.valueOf(buyGiveEntity.getPrice()) == minPrice) {
                                        int countGiveNum = Integer.valueOf(countBuyNum / productActiveThresholdEntity.getThresholdOne()) * productActiveThresholdEntity.getThresholdTwo();
                                        // 总共赠送数量
                                        //实体类转模型
                                        ShareBillBuyGiveModel productActiveModel = new ShareBillBuyGiveModel();
                                        productActiveModel.setRemarkId(buyGiveEntity.getRemarkId());
                                        productActiveModel.setRemark(buyGiveEntity.getRemark());
                                        productActiveModel.setProductId(buyGiveEntity.getProductId());
                                        productActiveModel.setSort(buyGiveEntity.getSort());
                                        productActiveModel.setPrice(buyGiveEntity.getPrice());
                                        productActiveModel.setMinNum(buyGiveEntity.getMinNum());
                                        productActiveModel.setImg(buyGiveEntity.getImg());
                                        productActiveModel.setProductExplain(buyGiveEntity.getProductExplain());
                                        productActiveModel.setPostage(buyGiveEntity.getPostage());
                                        productActiveModel.setPlantPrice(buyGiveEntity.getPlantPrice());
                                        productActiveModel.setCardImg(buyGiveEntity.getCardImg());
                                        productActiveModel.setInventory(buyGiveEntity.getInventory());
                                        productActiveModel.setMaxNum(buyGiveEntity.getMaxNum());
                                        productActiveModel.setThresholdId(buyGiveEntity.getThresholdId());
                                        productActiveModel.setComboImg(buyGiveEntity.getComboImg());
                                        productActiveModel.setShareBill(buyGiveEntity.getShareBill());
                                        productActiveModel.setShareBillRemarkId(buyGiveEntity.getShareBillRemarkId());
                                        productActiveModel.setProductName(buyGiveEntity.getProductName());
                                        productActiveModel.setCountGiveNum(countGiveNum);

                                        productRemarkEntity.setShareBillBuyGiveModel(productActiveModel);
                                    }
                                }
                            }

                            // 不拼单 满足送的要求
                            if (productRemarkEntity.getShareBillBuyGiveModel() == null && productRemarkEntity.getShareBill() == 0 && cartProduct.getCartBuyNum() >= productActiveThresholdEntity.getThresholdOne()) {
                                // 送的总量
                                int countGiveNum = 0;
                                countGiveNum = Integer.valueOf(cartProduct.getCartBuyNum() / productActiveThresholdEntity.getThresholdOne()) * productActiveThresholdEntity.getThresholdTwo();
                                // ProductRemarkEntity remarkEntity = new ProductRemarkEntity();

                                // 实体类转模型
                                BuyGiveModel productActiveModel = new BuyGiveModel();
                                productActiveModel.setRemarkId(productRemarkEntity.getRemarkId());
                                productActiveModel.setRemark(productRemarkEntity.getRemark());
                                productActiveModel.setProductId(productRemarkEntity.getProductId());
                                productActiveModel.setSort(productRemarkEntity.getSort());
                                productActiveModel.setPrice(productRemarkEntity.getPrice());
                                productActiveModel.setMinNum(productRemarkEntity.getMinNum());
                                productActiveModel.setImg(productRemarkEntity.getImg());
                                productActiveModel.setProductExplain(productRemarkEntity.getProductExplain());
                                productActiveModel.setPostage(productRemarkEntity.getPostage());
                                productActiveModel.setPlantPrice(productRemarkEntity.getPlantPrice());
                                productActiveModel.setCardImg(productRemarkEntity.getCardImg());
                                productActiveModel.setInventory(productRemarkEntity.getInventory());
                                productActiveModel.setMaxNum(productRemarkEntity.getMaxNum());
                                productActiveModel.setThresholdId(productRemarkEntity.getThresholdId());
                                productActiveModel.setComboImg(productRemarkEntity.getComboImg());
                                productActiveModel.setShareBill(productRemarkEntity.getShareBill());
                                productActiveModel.setShareBillRemarkId(productRemarkEntity.getShareBillRemarkId());
                                productActiveModel.setProductName(productRemarkEntity.getProductName());
                                productActiveModel.setCountGiveNum(countGiveNum);
                                // 将该赠品置入商品规格中
                                productRemarkEntity.setBuyGiveModel(productActiveModel);

                            }
                            break;
                        // 送代金券
                        case 2:
                            // 根据商品规格id查询赠出的代金券信息 thresholdThree 代金券类型id
                            Integer couponTypeId = productActiveThresholdEntity.getThresholdThree();
                            CouponTypeEntity couponTypeEntity = couponTypeMapper.selectByPrimaryKey(couponTypeId);
                            // 查询阈值信息
                            // 重新设置活动名字
                            productActiveTypeEntity.setActiveTypeName(productActiveThresholdEntity.getActiveName());

                            // 该商品允许拼单
                            if (productRemarkEntity.getShareBill() == 1) {
                                shareBillRemarks += cartProduct.getCartBuyNum();
                                // 查询那些商品可以与该商品拼单
                                String shareBillRemarkId = productRemarkEntity.getShareBillRemarkId();
                                for (CartProductEntity cartRemark : cartProductEntities) {
                                    // 该商品可以拼单
                                    if (shareBillRemarkId.equals(cartRemark.getProductRemarkId())) {
                                        couponEntities.add(couponTypeEntity);
                                        shareBillRemarks += cartRemark.getCartBuyNum();
                                    }
                                }

                            }
                            // 拼单满足送的要求（覆盖单体满足要求赠送的）
                            if (couponEntities != null && shareBillRemarks >= productActiveThresholdEntity.getThresholdOne()) {
                                shareBillCoupon = true;
                                // 代金券满足条件只送一张
                                productRemarkEntity.setShareBillCouponEntity(couponTypeEntity);

                            }
                            // 不拼单并且购买数量达到赠送值
                            if (productRemarkEntity.getShareBillCouponEntity() == null && productRemarkEntity.getShareBill() == 0 && cartProduct.getCartBuyNum() >= productActiveThresholdEntity.getThresholdOne()) {
                                // 代金券满足条件只送一张
                                // 将代金券信息置入商品规格中
                                productRemarkEntity.setCouponEntity(couponTypeEntity);
                            }
                            break;
                        // 换购
                        case 3:
                            // 重新设置活动名字
                            productActiveTypeEntity.setActiveTypeName(productActiveThresholdEntity.getActiveName());
                            // 查询换购商品信息
                            if (cartProduct.getProductStatus() == 1) {
                                String tradRemarkId = cartProduct.getTradRemarkId();
                                ProductRemarkEntity remarkEntity = productRemarkMapper.selectByPrimaryKey(tradRemarkId);
                                // 查询商品信息
                                ProductEntity product = productMapper.selectByPrimaryKey(remarkEntity.getProductId());
                                // 实体类转模型
                                TradActiveModel tradActiveMode = new TradActiveModel();
                                tradActiveMode.setRemarkId(remarkEntity.getRemarkId());
                                tradActiveMode.setRemark(remarkEntity.getRemark());
                                tradActiveMode.setProductId(remarkEntity.getProductId());
                                tradActiveMode.setSort(remarkEntity.getSort());
                                tradActiveMode.setPrice(remarkEntity.getPrice());
                                tradActiveMode.setMinNum(remarkEntity.getMinNum());
                                tradActiveMode.setImg(remarkEntity.getImg());
                                tradActiveMode.setProductExplain(remarkEntity.getProductExplain());
                                tradActiveMode.setPostage(remarkEntity.getPostage());
                                tradActiveMode.setPlantPrice(remarkEntity.getPlantPrice());
                                tradActiveMode.setCardImg(remarkEntity.getCardImg());
                                tradActiveMode.setInventory(remarkEntity.getInventory());
                                tradActiveMode.setMaxNum(remarkEntity.getMaxNum());
                                tradActiveMode.setThresholdId(remarkEntity.getThresholdId());
                                tradActiveMode.setComboImg(remarkEntity.getComboImg());
                                tradActiveMode.setShareBill(remarkEntity.getShareBill());
                                tradActiveMode.setProductName(product.getProductName());
                                tradActiveMode.setCountGiveNum(productActiveThresholdEntity.getThresholdThree());
                                // 再该商品规格下置入换购商品规格
                                productRemarkEntity.setTradActiveModel(tradActiveMode);
                                // 该商品规格拥有换购商品

                            }

                            break;
                        // 限时免邮费
                        case 4:
                            productRemarkEntity.setPostage("限时免邮费");
                            break;
                        // 赠品
                        case 5:
                            // 根据商品规格id查询赠品信息
                            ProductActiveGiveEntity activeGiveEntity = productMapper.queryGiveRemark(cartProduct.getProductRemarkId());
                            // 重新设置活动名字
                            productActiveTypeEntity.setActiveTypeName(productActiveThresholdEntity.getActiveName());
                            // 该商品规格购买数量达到送赠品要求
                            if (cartProduct.getCartBuyNum() >= productActiveThresholdEntity.getThresholdOne()) {
                                productRemarkEntity.setGiveEntity(activeGiveEntity);
                            }
                            break;
                    }
                    // 将阈值置入商品规格中
                    productRemarkEntity.setProductActiveThresholdEntity(productActiveThresholdEntity);
                }else{
                    // 活动过期了
                    productRemarkEntity.setThresholdId(0);
                }
            }

            // 将该规格的购买数量置入商品规格中
            productRemarkEntity.setCartBuyNum(cartProduct.getCartBuyNum());
            productEntities.add(productEntity);
        }
        // 拼单商品赠送覆盖不拼单赠送商品(买N送N)
        if (shareBillGiveBuy != null && shareBillGiveBuy == true) {
            productRemarkEntity.setBuyGiveModel(null);
        }
        // 拼单商品赠送覆盖不拼单赠送商品(买N送N代金券)
        if (shareBillCoupon != null && shareBillCoupon == true) {
            productRemarkEntity.setCouponEntity(null);
        }

        cartMessageModel.setCountMoney(countMoney);
        cartMessageModel.setProductEntities(productEntities);
        cartMessageModel.setCartId(cart.getCartId());
        return new ResponseResult(ResponseMessage.OK, cartMessageModel);
    }

    /**
     * 用户添加商品到购物车
     *
     * @param addUserCartModel
     * @return
     */
    @Transactional
    public ResponseResult addUserCart(List<AddUserCartModel> addUserCartModel) throws Exception {
        for (AddUserCartModel userCartModel : addUserCartModel) {
            String userId = userCartModel.getUserId();
            Integer cartBuyNum = userCartModel.getCartBuyNum();
            String productId = userCartModel.getProductId();
            String remarkId = userCartModel.getRemarkId();
            Integer productStatus = userCartModel.getProductStatus();
            String tradRemarkId = userCartModel.getTradRemarkId();
            // 查询用户的购物车
            Example cartExample = new Example(CartEntity.class);
            cartExample.createCriteria().andEqualTo("userId", userId).andEqualTo("delFlag", "0");
            CartEntity cartEntity = cartMapper.selectOneByExample(cartExample);
            // 如果用户没有购物车，为用户创建一个空的购物车
            if (cartEntity == null) {
                CartEntity cart = new CartEntity();
                cart.setUserId(userId);
                cart.setDelFlag(0);
                cartMapper.insert(cart);
                // 查询刚刚插入的购物车id
                CartEntity cartEntity1 = cartMapper.selectOneByExample(cartExample);
                // 向空购物车中放入商品
                CartProductEntity cartProductEntity = new CartProductEntity();
                cartProductEntity.setCartId(cartEntity1.getCartId());
                cartProductEntity.setCartBuyNum(cartBuyNum);
                cartProductEntity.setProductId(productId);
                cartProductEntity.setProductRemarkId(remarkId);
                cartProductEntity.setSelectFlag(1);
                if (productStatus != null) {
                    cartProductEntity.setProductStatus(productStatus);
                } else {
                    cartProductEntity.setProductStatus(0);
                }

                if (tradRemarkId != null) {
                    cartProductEntity.setTradRemarkId(tradRemarkId);
                }
                cartProductMapper.insert(cartProductEntity);
                return new ResponseResult(ResponseMessage.OK);
            }
            // 查询用户购物车中商品
            Example cartProductExample = new Example(CartProductEntity.class);
            cartProductExample.createCriteria().andEqualTo("cartId", cartEntity.getCartId());
            List<CartProductEntity> cartProductEntities = cartProductMapper.selectByExample(cartProductExample);
            boolean ifExist = false;
            // 用户购物车中有商品
            if (cartProductEntities != null && cartProductEntities.size() > 0) {
                for (CartProductEntity cartProduct : cartProductEntities) {
                    //  购物车中有该规格的商品
                    if (cartProduct.getProductRemarkId().equals(remarkId)) {
                        ifExist = true;
                        // 修改该商品规格的数量
                        Example cartProductExample2 = new Example(CartProductEntity.class);
                        cartProductExample2.createCriteria().andEqualTo("cartId", cartProduct.getCartId()).andEqualTo("productRemarkId", cartProduct.getProductRemarkId());
                        CartProductEntity cartProductEntity = new CartProductEntity();
                        cartProductEntity.setCartBuyNum(cartProduct.getCartBuyNum() + cartBuyNum);
                        cartProductEntity.setSelectFlag(1);

                        if(userCartModel.getProductStatus() != null){
                            cartProductEntity.setProductStatus(1);
                        }
                        if(userCartModel.getTradRemarkId() != null){
                            cartProductEntity.setTradRemarkId(userCartModel.getTradRemarkId());
                        }
                        cartProductMapper.updateByExampleSelective(cartProductEntity, cartProductExample2);
                    }
                }
            }

            if (ifExist == false) {
                // 购物车中没有该商品规格
                CartProductEntity cartProductEntity = new CartProductEntity();
                cartProductEntity.setCartId(cartEntity.getCartId());
                cartProductEntity.setCartBuyNum(cartBuyNum);
                cartProductEntity.setProductId(productId);
                cartProductEntity.setProductRemarkId(remarkId);
                cartProductEntity.setSelectFlag(1);
                if (productStatus != null) {
                    cartProductEntity.setProductStatus(productStatus);
                } else {
                    cartProductEntity.setProductStatus(0);
                }
                if (tradRemarkId != null) {
                    cartProductEntity.setTradRemarkId(tradRemarkId);
                }
                cartProductMapper.insert(cartProductEntity);
            }
        }

        return new ResponseResult(ResponseMessage.OK, "添加成功");
    }

    /**
     * 用户修改购物车商品规格数量
     *
     * @param cartProduct
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updateCartBuyNum(CartProductEntity cartProduct) throws Exception {
        // 单个选
        if (cartProduct.getAllSelect() == null) {
            if (cartProduct.getProductRemarkId() == null && cartProduct.getCartId() == null) {
                return new ResponseResult(ResponseMessage.NULL_REQUEST);
            }
            if (cartProduct.getSelectFlag() != null)
                cartProduct.setSelectFlag(cartProduct.getSelectFlag());
            if (cartProduct.getCartBuyNum() != null)
                cartProduct.setCartBuyNum(cartProduct.getCartBuyNum());
            // 单个选
            Example example = new Example(CartProductEntity.class);
            example.createCriteria().andEqualTo("productRemarkId", cartProduct.getProductRemarkId()).andEqualTo("cartId", cartProduct.getCartId());
            int integer = cartProductMapper.updateByExampleSelective(cartProduct, example);
            if (integer > 0)
                return new ResponseResult(ResponseMessage.OK);
            return new ResponseResult(ResponseMessage.UPDATE_FAIL);
        } else {
            // 全选 / 全不选
            if (cartProduct.getCartId() == null) {
                return new ResponseResult(ResponseMessage.NULL_REQUEST, "购物车id不能为空");
            }
            cartProduct.setSelectFlag(cartProduct.getAllSelect());
            Example example = new Example(CartProductEntity.class);
            example.createCriteria().andEqualTo("cartId", cartProduct.getCartId());
            int i = cartProductMapper.updateByExampleSelective(cartProduct, example);
            if (i > 0)
                return new ResponseResult(ResponseMessage.OK);
            return new ResponseResult(ResponseMessage.UPDATE_FAIL);
        }
    }

    /**
     * 用户删除购物车商品
     *
     * @param cartId
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult delUserCartRemark(Integer cartId) throws Exception {
        // 查询购物车中选中的商品规格
        Example example = new Example(CartProductEntity.class);
        example.createCriteria().andEqualTo("cartId", cartId);
        List<CartProductEntity> cartProductEntities = cartProductMapper.selectByExample(example);
        int i = 0;
        for (CartProductEntity cartProductEntity : cartProductEntities) {
            // 商品规格是选中的
            if (cartProductEntity.getSelectFlag() == 1) {
                Example cartProductExample = new Example(CartProductEntity.class);
                cartProductExample.createCriteria().andEqualTo("cartId", cartId).andEqualTo("productRemarkId", cartProductEntity.getProductRemarkId());
                i = cartProductMapper.deleteByExample(cartProductExample);
            }
        }
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 删除换购商品
     *
     * @param cartProductId
     * @return
     * @throws Exception
     */
    public ResponseResult delTradProduct(Integer cartProductId) throws Exception {
        CartProductEntity cartProductEntity = cartProductMapper.selectByPrimaryKey(cartProductId);
        cartProductEntity.setProductStatus(0);
        cartProductEntity.setTradRemarkId("");
        int i = cartProductMapper.updateByPrimaryKeySelective(cartProductEntity);
        if(i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }
}
